package com.ruoyi.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.app.dao.UserAuthMapper;
import com.ruoyi.app.entity.UserAuth;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.vo.EditAuthReq;
import com.ruoyi.web.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TUserMapper;
import com.ruoyi.system.domain.TUser;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.ObjectUtils;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-20
 */
@Service
public class TUserServiceImpl implements ITUserService {
    @Autowired
    private TUserMapper tUserMapper;


    @Autowired
    private UserAuthMapper userAuthMapper;


    @Value("${filegetpath}")
    private String pathPic;

    @Value("${defaultpicture}")
    private String defaultpicture;


    /**
     * 查询用户
     *
     * @param id 用户ID
     * @return 用户
     */
    @Override
    public Map<String, String> selectTUserById(String id) {
        Map<String, String> map = tUserMapper.selectTUserById(id);
        if (!ObjectUtils.isEmpty(map.get("realHouseStatePic"))) {
            map.put("realHouseStatePic", pathPic + map.get("realHouseStatePic"));
        } else {
            map.put("realHouseStatePic", defaultpicture);
        }
        if (!ObjectUtils.isEmpty(map.get("realCarStatePic"))) {
            map.put("realCarStatePic", pathPic + map.get("realCarStatePic"));
        } else {
            map.put("realCarStatePic", defaultpicture);
        }
        if (!ObjectUtils.isEmpty(map.get("realNameStatePic"))) {
            map.put("realNameStatePic", pathPic + map.get("realNameStatePic"));
        } else {
            map.put("realNameStatePic", defaultpicture);
        }
        return map;
    }

    /**
     * 查询用户列表
     *
     * @param tUser 用户
     * @return 用户
     */
    @Override
    public List<Map<String, String>> selectTUserList(TUser tUser) {
        List<Map<String, String>> list = tUserMapper.selectTUserList(tUser);

        List<Map<String, String>> newList = new ArrayList<>();
        for (Map<String, String> map : list) {
            Map<String, String> newMap = new HashMap<>();
            newMap.putAll(map);
            if (!ObjectUtils.isEmpty(map.get("headPortrait"))) {
                newMap.put("headPortrait", pathPic + map.get("headPortrait"));
            }
            newList.add(newMap);
        }
        return newList;
//        return list.stream().map(map -> {
//            Map<String, String> newMap = new HashMap<>();
//            newMap.putAll(map);
////            newMap.put("headPortrait", pathPic + map.get("headPortrait"));
//            return newMap;
//        }).collect(Collectors.toList());

    }

    /**
     * 新增用户
     *
     * @param tUser 用户
     * @return 结果
     */
    @Override
    public int insertTUser(TUser tUser) {
        tUser.setCreateTime(DateUtils.getNowDate());
        return tUserMapper.insertTUser(tUser);
    }

    /**
     * 修改用户
     *
     * @param tUser 用户
     * @return 结果
     */
    @Override
    public int updateTUser(TUser tUser) {
        tUser.setUpdateTime(DateUtils.getNowDate());
        return tUserMapper.updateTUser(tUser);
    }

    /**
     * 删除用户对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTUserByIds(String ids) {
        return tUserMapper.deleteTUserByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户信息
     *
     * @param id 用户ID
     * @return 结果
     */
    public int deleteTUserById(String id) {
        return tUserMapper.deleteTUserById(id);
    }


    @Override
    public int editAuth(EditAuthReq editAuthReq) {
        /**
         *      * 0: 未认证
         *      * 1: 审核中
         *      * 2: 审核失败
         *      * 3: 审核成功
         */
        UserAuth userAuth = userAuthMapper.selectById(editAuthReq.getId());
        if (userAuth == null) {
            throw new BusinessException("用户不存在！");
        }

        if (editAuthReq.getType().equals("0")) {
            // 实名
            if (userAuth.getRealNameState() != 1) {
                throw new BusinessException("当前状态不允许审核！");
            }

            userAuth.setRealNameState(editAuthReq.getStatus().equals("0") ? 3 : 2);
            userAuthMapper.updateById(userAuth);

        }

        if (editAuthReq.getType().equals("1")) {
            // 房子
            if (userAuth.getRealHouseState() != 1) {
                throw new BusinessException("当前状态不允许审核！");
            }

            userAuth.setRealHouseState(editAuthReq.getStatus().equals("0") ? 3 : 2);
            userAuthMapper.updateById(userAuth);
        }

        if (editAuthReq.getType().equals("2")) {
            // 车子

            if (userAuth.getRealCarState() != 1) {
                throw new BusinessException("当前状态不允许审核！");
            }

            userAuth.setRealCarState(editAuthReq.getStatus().equals("0") ? 3 : 2);
            userAuthMapper.updateById(userAuth);
        }
        return 1;
    }
}
